import axios from 'axios'
import {baseUrl} from "@/config/env";


let instance = axios.create();

instance.defaults.withCredentials = true;
instance.defaults.headers.common['Content-Type'] = 'application/json';
instance.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

const HEADERS = {
  CONTENT_TYPE: {
    JSON: {ContentType: "application/json; charset=utf-8"},
    X_WWW_FORM_URLENCODED: {ContentType: "application/x-www-form-urlencoded"},
  }
};

if (process.env.NODE_ENV === 'development') {
  instance.defaults.baseURL = baseUrl;
}

// 添加请求拦截器
instance.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    let token = window.localStorage.getItem('token');
    // 把token放到参数里面
    if (token && config.params) {
      config.params.token = token;
    } else {
      config.params = {'token': token};
    }
// 把token放到header里面
    if (token) {
      config.headers['token'] = token;
    }
    return config;
  },
  error => {
    let message = "参数错误";
    // 对请求错误做些什么
    let res = {};
    res.message = message;
    res.success = false;
    return Promise.reject(res);
  });

instance.interceptors.response.use(
  // 对响应数据做些事
  response => {
    let res = {};
    res.success = false;
    res.status = response.status;
    res.data = response.data;
    if (200 === response.status) {
      res.success = true;
    }
    return res;
  },
  error => {
    let message;
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          message = '错误请求'
          break;
        case 401:
          message = '未授权，请重新登录'
          break;
        case 403:
          message = '拒绝访问'
          break;
        case 404:
          message = '请求错误,未找到该资源'
          break;
        case 405:
          message = '请求方法未允许'
          break;
        case 408:
          message = '请求超时'
          break;
        case 500:
          message = '服务器端出错'
          break;
        case 501:
          message = '网络未实现'
          break;
        case 502:
          message = '网络错误'
          break;
        case 503:
          message = '服务不可用'
          break;
        case 504:
          message = '网络超时'
          break;
        case 505:
          message = 'http版本不支持该请求'
          break;
        default:
          message = `连接错误${error.response.status}`
      }
    } else {
      message = "连接到服务器失败"
    }

    let res = {};
    let data = {};
    res.message = message;
    data.message = message;
    res.status = error.response.status;
    res.success = false;
    res.data = data;
    return Promise.resolve(res);
  }
);

const send = function (url, data = {params: {}, data: {}}, method = 'GET', headers = {}) {
  return new Promise((resolve, reject) => {
    url = baseUrl + url;
    method = method.toUpperCase();

    let requestConfig = {};
    requestConfig.url = url;
    requestConfig.method = method;
    if (data.params && JSON.stringify(data.params) !== '{}') {
      requestConfig.params = data.params;
    }
    if (data.data && JSON.stringify(data.data) !== '{}') {
      requestConfig.data = data.data;
    }
    if (headers && JSON.stringify(headers) !== '{}') {
      requestConfig.headers = headers;
    }

    instance(requestConfig).then(response => {
        resolve(response);
      }
    ).catch(error => {
      resolve(error);
    });
  });
}

const http = {send: send, HEADERS: HEADERS};

export default http;


